//
//  ImageShowCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class ShowImageViewCell: UITableViewCell {
    static var identifier = "ImageShowCell.swordjoy"
    static var height: CGFloat = 150
    
    var models: [String] = [] {
        didSet {
            if models.isEmpty {
                addLabel.isHidden = true
                collectionView.isHidden = true
                return
            }
            else {
                addLabel.isHidden = false
                collectionView.isHidden = false
            }
            collectionView.reloadData()
        }
    }
    var titleName: String = "" {
        didSet {
            addLabel.text = titleName.isEmpty ? "附件(选填)" : titleName
        }
    }
    
    var cellDidClick: ( (Int, UICollectionView) -> () )?
    
    fileprivate var addLabel: UILabel!
    fileprivate var collectionView: UICollectionView!
    lazy var mainView:UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.cornerRadius = 4
        mainView.clipsToBounds = true
        self.addSubview(mainView)
        
        mainView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalToSuperview()
            make.width.equalTo(UIScreen.width-20)
        }
        
        return mainView
    }()
    
    // MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.white
        selectionStyle = .none
        create()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - fileprivate

extension ShowImageViewCell {
    fileprivate func create() {
        backgroundColor = UIColor.clear
        
        addLabel = UILabel()
        mainView.addSubview(addLabel)
        addLabel.font = GlobalConfig.system16Font
        addLabel.textColor = GlobalConfig.TextDeepColor
        addLabel.text = titleName.isEmpty ? "附件(选填)" : titleName
        addLabel.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.top.equalTo(10)
        }
        
        let flow = UICollectionViewFlowLayout()
        let width = (UIScreen.width - 100) / 4
        flow.itemSize = CGSize(width: width, height: width)
        flow.minimumInteritemSpacing = 10
        
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
        collectionView.backgroundColor = UIColor.clear
        collectionView.contentInset = UIEdgeInsets(top: 15, left: 20, bottom: 0, right: 20)
        mainView.addSubview(collectionView)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.bounces = false
        collectionView.isScrollEnabled = false
        collectionView.register(ImageSelectCell.self, forCellWithReuseIdentifier: ImageSelectCell.identifier)
        collectionView.register(ChooseImageCell.self, forCellWithReuseIdentifier: ChooseImageCell.identifier)
        collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(addLabel.snp.bottom)
            make.bottom.equalTo(-10)
            make.height.equalTo(width+10)
        }
        
    }
}

extension ShowImageViewCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageSelectCell.identifier, for: indexPath) as! ImageSelectCell
        cell.imageName = models[indexPath.row]
        cell.isHideDeleteButton = true
        
        return cell
        
    }
}

extension ShowImageViewCell: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var imageViews: [UIImageView] = []
        let cells = collectionView.visibleCells
        for cell in cells {
            let temp = cell as! ImageSelectCell
            imageViews.append(temp.imageV)
        }
        
        cellDidClick?(indexPath.row, collectionView)
    }
    
}


